home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13956 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.3 KB  |  275 lines

  1. Path: unsw.edu.au!usenet
  2. From: jeroen@fatboy.gas.unsw.edu.au (Jeroen Dijkmeijer)
  3. Newsgroups: comp.lang.c++
  4. Subject: template problem
  5. Date: 28 Mar 1996 05:51:59 GMT
  6. Organization: Centre for Remote Sensing and GIS, UNSW.
  7. Message-ID: <4jd9dv$27g@mirv.unsw.edu.au>
  8. Reply-To: jeroen@fatboy.gas.unsw.edu.au
  9. NNTP-Posting-Host: wideglide.geog.unsw.edu.au
  10. Keywords: template, friend declaration.
  11.  
  12. Problem with templates and friend declaration
  13.   G'day
  14.  
  15. I haven't much of C++ experience. But I'm studying hard to learn. At this moment I do have a problem with templates. The example is taken from Deitel & Deitel C++ how to program ch 15 pg 697
  16.  
  17. The files are listed below.
  18.  
  19.  
  20. // file listnode.h
  21. // List template definition
  22.  
  23. #ifndef LISTNODE_H
  24. #define LISTNODE_H
  25.  
  26. template<class DATA_TYPE>
  27. class ListNode {  
  28.   friend class List<DATA_TYPE>;
  29. public:
  30.   ListNode(const DATA_TYPE &); //constructor
  31.   DATA_TYPE getData() const;
  32. private:
  33.   DATA_TYPE data;              // data
  34.   ListNode *nextPtr;
  35. };
  36.  
  37. template<class DATA_TYPE>
  38. ListNode<DATA_TYPE>::ListNode(const DATA_TYPE &info)
  39. {
  40.   data = info;
  41.   nextPtr = NULL;
  42. }
  43.  
  44. template<class DATA_TYPE>
  45. DATA_TYPE ListNode<DATA_TYPE>::getData() const 
  46. { return data; }
  47. #endif
  48.  
  49.  
  50.  
  51. // file list.h
  52. // Template definition of a List class.
  53.  
  54. #ifndef LIST_H
  55. #define LIST_H
  56. #include <iostream.h>
  57. #include <assert.h>
  58. #include "listnode.h"
  59.  
  60. template<class DATA_TYPE>
  61. class List {
  62. public:
  63.   List(); //constructor
  64.   ~List(); //destructor
  65.   void insertAtFront(const DATA_TYPE &);
  66.   int removeFromFront(DATA_TYPE &);
  67.   int isEmpty() const;
  68.   int member(const DATA_TYPE &) const;
  69.   void print() const;
  70. private:
  71.   ListNode <DATA_TYPE> *firstPtr;
  72.   ListNode <DATA_TYPE> *lastPtr;
  73.   ListNode <DATA_TYPE> *getNewNode(const DATA_TYPE &);
  74. };
  75.  
  76. //---------------------------------------- member definitions for List
  77. template<class DATA_TYPE>
  78. List<DATA_TYPE>::List()
  79. {firstPtr = lastPtr = NULL; }
  80.  
  81. // Destructor
  82. template<class DATA_TYPE>
  83. List<DATA_TYPE>::~List()
  84. {
  85.   for(int i=0;i<10;i++);
  86.   if(!isEmpty()) {
  87.     cout<<"Destroying nodes ... "<<endl;
  88.     ListNode<DATA_TYPE> *currentPtr = firstPtr, *tempPtr;
  89.     while (currentPtr != NULL) {
  90.       tempPtr = currentPtr;
  91.       cout <<"deleting "<< tempPtr->data << endl;
  92.       currentPtr = currentPtr -> nextPtr;
  93.       delete tempPtr;
  94.     }
  95.   }
  96.   cout << "All nodes destroyed" << endl <<endl;
  97. }
  98.  
  99. // Insert a node at the front of the list
  100. template<class DATA_TYPE>
  101. void List<DATA_TYPE>::insertAtFront(const DATA_TYPE &value)
  102. {
  103.   ListNode<DATA_TYPE> *newPtr = getNewNode(value);
  104.  
  105.   if (isEmpty())
  106.     firstPtr = lastPtr = newPtr;
  107.   else {
  108.     newPtr->nextPtr = firstPtr;
  109.     firstPtr = newPtr;
  110.   }
  111. }
  112.  
  113. template<class DATA_TYPE>
  114. int List<DATA_TYPE>::removeFromFront(DATA_TYPE &info)
  115. {
  116.   if (isEmpty())
  117.     return 0;
  118.   else {
  119.     ListNode<DATA_TYPE> *tempPtr = firstPtr;
  120.     if (firstPtr == lastPtr)
  121.       firstPtr = lastPtr = 0;
  122.     else
  123.       firstPtr = firstPtr->nextPtr;
  124.     info = tempPtr->data;
  125.     delete tempPtr;
  126.     return 1;
  127.   }
  128. }  // RemoveFromFront(DATA_TYPE &info)
  129.  
  130. template<class DATA_TYPE>
  131. int List<DATA_TYPE>::isEmpty() const {return firstPtr == NULL; }
  132.  
  133. template<class DATA_TYPE>
  134. ListNode<DATA_TYPE> *List<DATA_TYPE>::getNewNode(const DATA_TYPE &value)
  135. {
  136.   ListNode<DATA_TYPE> *ptr = new ListNode<DATA_TYPE>(value);
  137.   assert(ptr != 0);
  138.   return ptr;
  139. }  // getNewNode(const DATA_TYPE &data)
  140.  
  141. // Display the contents of the List
  142. template<class DATA_TYPE>
  143. void List<DATA_TYPE>::print() const
  144. {
  145.   if(isEmpty()) {
  146.     cout << "The list is empty" << endl <<endl;
  147.     return;
  148.   }
  149.   ListNode<DATA_TYPE> *currentPtr = firstPtr;
  150.   cout << "The List is: ";
  151.   while (currentPtr != 0) {
  152.     cout <<currentPtr->data << ' ';
  153.     currentPtr = currentPtr->nextPtr;
  154.   }
  155.   cout << endl << endl;
  156. }
  157.  
  158. template<class DATA_TYPE>
  159. int List<DATA_TYPE>::member(const DATA_TYPE &info) const {
  160.   if(isEmpty()) return 0;
  161.   else {
  162.     ListNode<DATA_TYPE> *currentPtr = firstPtr;
  163.     while(currentPtr != NULL) {
  164.       if(currentPtr->data == info) return 1;
  165.       currentPtr = currentPtr->nextPtr;
  166.     }
  167.     return 0;
  168.   }
  169. }
  170. #endif
  171.  
  172.  
  173. // file list_driver.cpp
  174. #include <iostream.h>
  175. #include "list.h"
  176. #include "pair.h"
  177.  
  178. void testIntegerList();
  179. void testPairList();
  180.  
  181. main()
  182. {
  183.   testIntegerList();
  184.   testPairList();
  185.  
  186.   return 0;
  187. }
  188.  
  189. void testIntegerList()
  190. {
  191.   cout << "testing a list of integer values" << endl;
  192.   List<int> integerList;
  193.   
  194.   int choice, value;
  195.   do {
  196.     cout << "? ";
  197.     cin >> choice;
  198.     switch (choice) {
  199.     case 1:
  200.       cout << "Enter an integer: ";
  201.       cin >> value;
  202.       integerList.insertAtFront(value);
  203.       integerList.print();
  204.       break;
  205.     case 2:
  206.       cout << "Deleting a node"<<endl;
  207.       integerList.removeFromFront(value);
  208.       cout<<"The value was"<<value<<endl;
  209.       break;
  210.     case 3:
  211.       cin>>value;
  212.       if(integerList.member(value)) cout<<value<<"is element of list"<<endl;
  213.       else cout<<value<<"is NOT a member of list"<<endl;
  214.       break;
  215.     }
  216.   } while (choice != 4);
  217.   cout << "End test of integerList" <<endl;
  218. }
  219.  
  220. void testPairList()
  221. {
  222.   cout << "testing a list of pair values" << endl;
  223.   List<Pair> pairList;
  224.   
  225.   int choice;
  226.     Pair value;
  227.   do {
  228.     cout << "? ";
  229.     cin >> choice;
  230.     switch (choice) {
  231.     case 1:
  232.       cout << "Enter a pair: ";
  233.       cin >> value;
  234.       pairList.insertAtFront(value);
  235.       pairList.print();
  236.       break;
  237.     case 2:
  238.       cout << "Deleting a node"<<endl;
  239.       pairList.removeFromFront(value);
  240.       cout<<"The value was"<<value<<endl;
  241.       break;
  242.     case 3:
  243.       cin>>value;
  244.       if(pairList.member(value)) cout<<value<<"is element of list"<<endl;
  245.       else cout<<value<<"is NOT a member of list"<<endl;
  246.       break;
  247.     }
  248.   } while (choice != 4);
  249.   cout << "End test of pairList" <<endl;
  250. }
  251.  
  252. void instructions()
  253. {
  254.   cout<<"Your choices are:"<<endl;
  255.   cout<<"1: Add an value to the list"<<endl;
  256.   cout<<"2: remove head from the list"<<endl;
  257.   cout<<"3: see if an entered value is member of the list"<<endl;
  258.   cout<<"4: quit the whole damned program"<<endl<<endl;
  259.   cout<<"Enter your choice (Please)"<<endl;
  260. }
  261.  
  262. wideglide[~/c_programs/c++ ]6%CC -g -fullwarn -o list_driver list_driver.cpp
  263. CC -g -fullwarn -o list_driver list_driver.cpp
  264. "listnode.h", line 9: error(3133): expected an identifier
  265.     friend class List<DATA_TYPE>;
  266.                      
  267.  
  268. "listnode.h", line 9: error(3358): invalid friend declaration
  269.     friend class List<DATA_TYPE>;
  270.     
  271. 2 errors detected in the compilation of "list_driver.cpp".
  272. wideglide[~/c_programs/c++ ]7% 
  273.  
  274.  
  275.